package com.xinyue.authorization.dao.impl;

import com.xinyue.authorization.bean.Action;
import com.xinyue.authorization.dao.ActionDao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository("ActionDaoImpl")
public class ActionDaoImpl implements ActionDao {
	@Autowired
    private SessionFactory factory;
    
	//默认方法
	public void save(Action entity) {
		factory.getCurrentSession().save(entity);
	}
	
	public Integer deleteById(int actionId) {
		Query q = factory.getCurrentSession().createQuery("delete Action where actionId=?");
		q.setParameter(0, actionId);
		return q.executeUpdate();
	}
	
	public void update(Action entity) {
		factory.getCurrentSession().update(entity);
	}
	
	public Action queryById(int actionId) {
		Query q = factory.getCurrentSession().createQuery("from Action where actionId=?");
		q.setParameter(0, actionId);
		return (Action) q.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	public List<Action> queryAll(int begin, int count) {
		Query q = factory.getCurrentSession().createQuery("from Action");
		q.setFirstResult(begin);
		q.setMaxResults(count);
		return q.list();
	}
	
	public Integer queryCount() {
		return (Integer) factory.getCurrentSession().createQuery("select count(*) from Action").uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	public List<Action> queryByActionType(int actionType) {
		Query q = factory.getCurrentSession().createQuery("from Action where actionType=:actionType");
		q.setInteger("actionType", actionType);
		return (List<Action>) q.list();
	};
}